package com.qfedu.edu.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaobobo
 * @title: MyFeignInterceptor
 * @projectName seckill-fy-2401-parent
 * @description: 简单的说 就是每一次发送请求Feign都会执行到这里来
 * 写这个拦截器的目的 就是为了token能级联传输
 * @date 2024/11/5  14:46
 */
@Component
public class MyFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //这里就要从上一个请求中拿到这个token然后从新放到这个requestTemplate中
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("token");
        String gatewayTag = request.getHeader("gatewayTag");
        //然后将这个token从新放到另一个请求中
        requestTemplate.header("token",token);
        requestTemplate.header("gatewayTag",gatewayTag);
    }
}
